package cn.hty.mymsg.handler.config;

import cn.hty.mymsg.common.pipeline.ProcessController;
import cn.hty.mymsg.common.pipeline.ProcessTemplate;
import cn.hty.mymsg.handler.action.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Configuration
@ComponentScan(basePackages = "cn.hty.mymsg")
public class TaskPipelineConfig {
    public static final String PIPELINE_HANDLER_CODE = "handler";
    @Autowired
    private DiscardAction discardAction;
    @Autowired
    private ShieldAction shieldAction;
    @Autowired
    private DeduplicationAction deduplicationAction;

    @Autowired
    private SendMessageAction sendMessageAction;

    /**
     * 组装流程
     * 0.丢弃消息
     * 1.屏蔽消息
     * 2.通用去重功能
     * 3.发送消息
     * @return
     */
    @Bean("taskTemplate")
    public ProcessTemplate taskTemplate(){
        ProcessTemplate processTemplate=new ProcessTemplate();
        processTemplate.setProcessList(Arrays.asList(discardAction, shieldAction, deduplicationAction,  sendMessageAction));
        return processTemplate;
    }

    /**
     * 流程控制器
     * @return
     */
    @Bean("handlerProcessController")
    public ProcessController processController(){
        ProcessController processController = new ProcessController();
        Map<String,ProcessTemplate> templateConfig=new HashMap<>();
        templateConfig.put(PIPELINE_HANDLER_CODE,taskTemplate());
        processController.setTemplateConfig(templateConfig);
        return processController;
    }
}
